package common.avatar
{
	import common.core.GSprite;
	import common.manager.Resource;
	
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.geom.Rectangle;
	import flash.utils.Dictionary;
	
	public class AvatarSprite extends GSprite
	{
		private var m_body:MovieClip ;
		
		public  static var  dectAvatar:Dictionary = new Dictionary(true);
		
		private var m_id : String;		
		private var m_modeId:String;
		
		private var m_oldStatu : String;
		private var m_curStatu : String;
		
		private var m_activeRange : Rectangle;  //活动范围
		
		private var m_clearAvatarMemory : Boolean;
		
		private var m_mass : Number; //质量
		
		public function AvatarSprite()
		{
		
		}
		
		public  function build(modeId:String,swfURL:String,clearMemory:Boolean=false):void
		{
			var path:String = swfURL;
			
			m_mass = 0;
			
			m_clearAvatarMemory = clearMemory;
			
			this.m_modeId = modeId;
			
			Resource.add(m_modeId,path);
			Resource.start(m_modeId,onComplete);
		}
		
		private function onComplete(o:Event):void
		{
			m_body = new (Resource.getClass(m_modeId,m_modeId,m_clearAvatarMemory)) as MovieClip ;
			
			dectAvatar[m_modeId] = m_body;
			
			curStatu = m_curStatu;
			
			this.addChild(m_body);
		
			m_body.addEventListener("returnOldStatu",returnOldStatu);
		}
		
		/**
		 * 设置熊猫的当前状态
		 * @param statu 
		 */		
		public function set curStatu(statu:String) : void
		{
			if(statu != m_curStatu)
			{
				m_oldStatu = m_curStatu;
				m_curStatu = statu;
			}
			
			if(m_body)
			{
				m_body.gotoAndStop(statu);
			}
		}
		
		/**
		 * 返回旧的状态 
		 */		
		public function returnOldStatu(evt : Event) : void
		{
			if(m_body)
			{
				m_curStatu = m_oldStatu;
				m_body.gotoAndStop(m_oldStatu);
			}
		}
		
		/**
		 * 获取活动范围
		 * @return  
		 */		
		public function get activeRange() : Rectangle
		{
			return m_activeRange;
		}
		
		/**
		 * 设置活动范围
		 * @return  
		 */
		public function set activeRange(range:Rectangle) : void
		{
			m_activeRange = range;
		}
		
		/**
		 * 获取当前的任务状态
		 * @return  
		 */				
		public function get curStatu() : String
		{
			return m_curStatu;
		}
		
		public function get body() : MovieClip
		{
			return m_body;
		}
		
		public function selectStatu(modeId:String,stata:String):void
		{
			if(dectAvatar[modeId])
			{
				var avatarRole:MovieClip = dectAvatar[modeId] as MovieClip;
				avatarRole.gotoAndPlay(stata);
			}
		}
		
		public  override function destory() : void
		{
			m_curStatu = null;
			m_modeId = null;
			
			m_body.removeEventListener("returnOldStatu",returnOldStatu);
		}
		
		public function set ID(id:String) : void
		{
			m_id = id;
		}
		
		public function get ID() : String
		{
			return m_id;
		}
		
		/**
		 * 刷新 
		 */		
		public override function update() : void
		{
			//限制在活动矩形范围类
			if(m_activeRange)
			{
				if(this.x < activeRange.left)
				{
					this.x = activeRange.left;
				}
				else if(this.x > this.activeRange.width)
				{
					this.x = this.activeRange.width;
				}
				
				if(this.y < activeRange.top)
				{
					this.y = activeRange.top;
				}
				else if(this.y > this.activeRange.height)
				{
					this.y = this.activeRange.height;
				}
			}
		}
	}
}